package com.etboot.modules.activiti.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.etboot.common.utils.SecurityUtil;
import com.etboot.common.vo.SearchVo;
import com.etboot.modules.activiti.dao.mapper.ActBusinessMapper;
import com.etboot.modules.activiti.entity.ActBusiness;
import com.etboot.modules.activiti.service.ActBusinessService;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 业务申请接口实现
 *
 * @author xboot
 */
@Slf4j
@Service
@Transactional
public class ActBusinessServiceImpl extends ServiceImpl<ActBusinessMapper, ActBusiness> implements ActBusinessService {
    @Autowired
    private ActBusinessMapper actBusinessMapper;

    @Autowired
    private SecurityUtil securityUtil;

    @Override
    public Page<ActBusiness> findByCondition(ActBusiness actBusiness, SearchVo searchVo, Page pageable) {
        QueryWrapper<ActBusiness> qw = new QueryWrapper<>();
        // 模糊搜素
        if (StrUtil.isNotBlank(actBusiness.getTitle())) {
            qw.like("title", actBusiness.getTitle());
        }
        // 状态
        if (actBusiness.getStatus() != null) {
            qw.eq("status", actBusiness.getStatus());
        }
        // 结果
        if (actBusiness.getResult() != null) {
            qw.eq("result", actBusiness.getResult());
        }
        // 创建时间
        if (StrUtil.isNotBlank(searchVo.getStartDate()) && StrUtil.isNotBlank(searchVo.getEndDate())) {
            Date start = DateUtil.parse(searchVo.getStartDate());
            Date end = DateUtil.parse(searchVo.getEndDate());
            qw.between("create_time", start, DateUtil.endOfDay(end));
        }
        // 用户
        qw.eq("user_id", securityUtil.getCurrUser().getId());
        qw.ne("proc_def_id", "");

        return actBusinessMapper.selectPage(pageable, qw);
    }

    @Override
    public List<ActBusiness> findByProcDefId(String procDefId) {
        return actBusinessMapper.selectList(Wrappers.<ActBusiness>lambdaQuery().eq(ActBusiness::getProcDefId, procDefId));
//        return actBusinessDao.findByProcDefId(procDefId);
    }
}